/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.internal.ui;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;
import org.eclipse.jface.viewers.LabelProvider;

import com.googlecode.tapestry.ide.TapestryPlugin;
import com.googlecode.tapestry.ide.core.model.IModelElement;
import com.googlecode.tapestry.ide.core.model.ITapestryModel;
import com.googlecode.tapestry.ide.core.model.ITapestryProject;
import com.googlecode.tapestry.ide.ui.TapestryImages;

/**
 * This decorator adds an overlay image to all Tapestry Projects.
 */
public class TapestryLabelDecorator extends LabelProvider implements
		ILightweightLabelDecorator {

	/** The Constant DECORATOR_ID. */
	public static final String DECORATOR_ID = TapestryPlugin.PLUGIN_ID
			+ ".ui.tapestryLabelDecorator";

	/** The tapestry model. */
	private ITapestryModel tapestryModel;

	/**
	 * Instantiates a new tapestry label decorator.
	 * 
	 * @param tapestryModel
	 *            the tapestry model
	 */
	public TapestryLabelDecorator(ITapestryModel tapestryModel) {
		this.tapestryModel = tapestryModel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(java.lang
	 * .Object, org.eclipse.jface.viewers.IDecoration)
	 */
	@Override
	public void decorate(Object element, IDecoration decoration) {
		// if (element instanceof IProject) {
		// decorateProject((IProject) element, decoration);
		// }
		if (element instanceof IFile) {
			decorateTapestryResource((IFile) element, decoration);
		}
		if (element instanceof IJavaElement) {
			decorateJavaElement((IJavaElement) element, decoration);
		}
		if (element instanceof IModelElement) {
			decorateTapestryElement(decoration);
		}
	}

	// public void decorateProject(IProject project, IDecoration decoration) {
	// if (TapestryCoreUtils.hasNature(project, TapestryPlugin.NATURE_ID)) {
	// decoration.addOverlay(TapestryImages.DESC_OVR_TAPESTRY,
	// IDecoration.TOP_LEFT);
	// }
	// }

	/**
	 * Decorate java element.
	 * 
	 * @param javaElement
	 *            the java element
	 * @param decoration
	 *            the decoration
	 */
	public void decorateJavaElement(IJavaElement javaElement,
			IDecoration decoration) {
		ITapestryProject project = tapestryModel.getProject(javaElement
				.getResource().getProject());
		if (project != null
				&& project.isTapestryResource(javaElement.getResource())) {
			decorateTapestryResource(javaElement.getResource(), decoration);
		}
	}

	/**
	 * Decorate tapestry resource.
	 * 
	 * @param resource
	 *            the resource
	 * @param decoration
	 *            the decoration
	 */
	public void decorateTapestryResource(IResource resource,
			IDecoration decoration) {
		decoration.addOverlay(TapestryImages.DESC_OVR_TAPESTRY,
				IDecoration.TOP_RIGHT);
	}

	/**
	 * Decorate tapestry element.
	 * 
	 * @param decoration
	 *            the decoration
	 */
	public void decorateTapestryElement(IDecoration decoration) {
		decorateTapestryResource(null, decoration);
	}

}
